package org.reborn.engine.app

import java.awt._

class FullscreenSupport {

  private var fullscreen = false
  
  def setFullScreenIfAvailable(frame:Frame){
    val graphicsEnv = GraphicsEnvironment.getLocalGraphicsEnvironment
    val displayDevice = graphicsEnv.getDefaultScreenDevice
    
    frame.setUndecorated(true)
    if(displayDevice.isFullScreenSupported){
      try{
		displayDevice.setFullScreenWindow(frame)
		fullscreen = true;
      }
      catch{
        case _ => {
          displayDevice.setFullScreenWindow(null)
          fullscreen = false;
        }
      }
	}
  }
  
  def isFullScreen():Boolean = {fullscreen}
  
  def leaveFullScreen(){
    if(fullscreen){
      val graphicsEnv = GraphicsEnvironment.getLocalGraphicsEnvironment
      val displayDevice = graphicsEnv.getDefaultScreenDevice
      
      displayDevice.setFullScreenWindow(null);
      fullscreen = false;
    }
  }
}

class DisplayModeSupport{
  private var displayModeChanged = false
  
  private var oldDisplayMode:DisplayMode = null
  private var currentDisplayMode:DisplayMode = null
  
  def changeDisplayMode(width:Int,height:Int,bitDepth:Int,refreshRate:Int){
    val graphicsEnv = GraphicsEnvironment.getLocalGraphicsEnvironment
    val displayDevice = graphicsEnv.getDefaultScreenDevice
    
    if(displayDevice.isDisplayChangeSupported){
      if(!displayModeChanged){
        oldDisplayMode = displayDevice.getDisplayMode
        currentDisplayMode = oldDisplayMode
        
        println("Changing Display Mode:");
        println("Old: ("+oldDisplayMode.getWidth+","+oldDisplayMode.getHeight+","+oldDisplayMode.getBitDepth+","+oldDisplayMode.getRefreshRate+" hertz)")
        println("New: ("+width+","+height+","+bitDepth+","+refreshRate+" hertz)")
        
        try{
          currentDisplayMode = new DisplayMode(width,height,bitDepth,refreshRate)
          displayDevice.setDisplayMode(currentDisplayMode)
          displayModeChanged = true
        }
        catch{
          case _ => {
            System.err.println("Error changing Display Mode. Reverting back to previous mode.")
            displayDevice.setDisplayMode(oldDisplayMode)
            displayModeChanged = false
          }
        }
      }
    }
    
  }
  
  def revertDisplayMode(){
    val graphicsEnv = GraphicsEnvironment.getLocalGraphicsEnvironment
    val displayDevice = graphicsEnv.getDefaultScreenDevice
    
    if(displayModeChanged){
      println("Reverting to original Display Mode.");
      displayDevice.setDisplayMode(oldDisplayMode)
      displayModeChanged = false
    }
  }
}

